#!/bin/bash
if [ ! $# -eq 6 ]
then
	echo "Usage: <num_files_per_task> <base_file> <global_storage> <rounds> <falkon_path> <nodes>"
	exit 1
fi

num_files_per_task=$1
base_file=$2
global_storage=$3
rounds=$4
falkon_path=$5
nodes=$6

if [ ! -f $nodes ]
then
	echo "file " $nodes " does not exist"
	exit 1
fi
source ./falkon_common.sh

num_lines=`cat ${nodes} | wc -l` 
n_ex_nodes=`echo "${num_lines}-2" | bc`
num_files=`echo "${num_files_per_task}*${n_ex_nodes}" | bc`
input_list="input_files.list"$RANDOM	

# generating the input files
copy_file $num_files $base_file $global_storage $input_list

job_file=`python vanillajob.py ${num_files_per_task} ${global_storage} < ${input_list}`

cp $job_file $global_storage
job_file=$global_storage/`basename ${job_file}`
echo "job file" $job_file

#killing zombie processes
killjava $nodes

#starting dispatch service
service_node=`head -n 1 < ${nodes}`
client_node=`head -n 2 < ${nodes} | tail -n 1`
startservice $service_node $falkon_path

#starting workers 
echo "sleeping 10 secs before start workers"
sleep 10
worker_nodes="worker_nodes.txt"
tail -n $n_ex_nodes $nodes > $worker_nodes

startworkers $worker_nodes $falkon_path $service_node

sleep 10
for round in `seq ${rounds}`
do	
	runjob $job_file $service_node $client_node $falkon_path $n_ex_nodes
done

rm $input_list
rm $worker_nodes
